﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GN.Pay.Application.GatewayApp.Dto.Output;
using System.Drawing;
using GN.Pay.Utils;
using System.IO;
using GN.Pay.Application.GatewayApp;
using GN.Pay.GatewayWeb.Models;
using Abp.Auditing;
using GN.Pay.Application.GatewayApp.Dto.Input;
using GN.Pay.TradeChannel;

namespace GN.Pay.GatewayWeb.Controllers.PayRoute
{
    /// <summary>
    /// 具有二维码支付的路油
    /// </summary>
    abstract class AQrCodePayActionRoute : APayActionRoute
    {
        protected AQrCodePayActionRoute(IClientInfoProvider clientInfo,
            ITradeApplyAppService service) : base(clientInfo, service)
        {
        }

        /// <summary>
        /// 二维码内存图片Url
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        protected string QRCodeMemoryImagePath(string code)
        {
            using (Bitmap bitmap = QrCodeHelper.Create(code))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                    return "data:image/gif;base64," + Convert.ToBase64String(ms.ToArray());
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="output"></param>
        /// <returns></returns>
        protected QRCodePayModel GetQRCodePayModel(PayChannelOutput output)
        {
            var apply = service.CashiePayApply(new CashiePayApplyInput() { ApplyId = output.ApplyId, ChannelType = ChannelType.None });
            if (apply == null)
            {
                throw new InvalidOperationException("申请无效，可能订单已失效或已支付");
            }
            var model = new QRCodePayModel()
            {
                ApplyId = output.ApplyId,
                ApplyTime = apply.ApplyTime,
                Body = apply.Body,
                ChannelId = output.ChannelId,
                OrderNo = apply.OrderNo,
                OrderTime = apply.OrderTime,
                PayId = output.PayId,
                PayMoney = apply.OrderMoney,
                Remark = apply.Remark,
                Subject = apply.Subject,
                TradeNo = output.TradeNo,
                AppName = apply.AppName,
                CashierName = apply.CashierName,
                LogoUrl = apply.LogoUrl,
                MerchantName = apply.MerchantName,
                MerchantType = apply.MerchantType,
                ExpireMessage = apply.ExpireMessage
            };
            string url;
            if (!string.IsNullOrWhiteSpace(output.PayUrl) && !string.IsNullOrWhiteSpace(output.PayParams))
            {
                url = output.PayUrl + "&" + output.PayParams;
            }
            else if (!string.IsNullOrWhiteSpace(output.PayUrl))
            {
                url = output.PayUrl;
            }
            else
            {
                url = output.PayParams;
            }
            model.QrCodePath = QRCodeMemoryImagePath(url);
            return model;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="output"></param>
        /// <returns></returns>
        protected override ActionResult ViewResult(PayChannelOutput output, Func<string, object, ActionResult> viewResultFactory)
        {
            QRCodePayModel model = GetQRCodePayModel(output);
            return viewResultFactory(ViewName, model);
        }

        /// <summary>
        /// 获取视图名称
        /// </summary>
        protected abstract string ViewName { get; }


    }
}